package mortal.game.control
{
    import Message.Game.*;
    import flash.events.*;
    import flash.utils.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MailController extends Controller
    {
        private var _isViewShow:Boolean = false;
        private var _mailModule:MailModule;
        private static var _isHaveNew:Boolean = false;
        private static var _isFirstQuery:Boolean = true;

        public function MailController()
        {
            Dispatcher.addEventListener(EventName.MailSend, this.sendMail);
            Dispatcher.addEventListener(EventName.MailRemove, this.removeMail);
            Dispatcher.addEventListener(EventName.MailQuery, this.queryMail);
            Dispatcher.addEventListener(EventName.MailGetAttachment, this.getMailAttachment);
            Dispatcher.addEventListener(EventName.MailRead, this.readMail);
            Dispatcher.addEventListener(EventName.MailQuerySuccess, this.onMailQuerySuccess);
            Dispatcher.addEventListener(EventName.NewMailCome, this.onNewMailComeHandler);
            Dispatcher.addEventListener(EventName.MailSendToPlayer, this.onMailSendToPlayerHandler);
            
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        override protected function initView() : IView
        {
            if (this._mailModule == null)
            {
                this._mailModule = new MailModule();
                this._mailModule.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            if (this._mailModule.getSendPanelVisible())
            {
                cache.mail.isWriting = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.Mail_StatusChange));
            }
            return this._mailModule;
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            if (_isFirstQuery)
            {
                this._mailModule.setDefault();
                _isFirstQuery = false;
            }
            else if (_isHaveNew)
            {
                this._mailModule.setDefault();
            }
            if (_isHaveNew)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.NewMailHadQuery));
            }
            _isHaveNew = false;
            
        }

        override protected function initServer() : void
        {
            
        }

        private function getMailAttachment(event:DataEvent) : void
        {
            var _loc_2:* = event.data["mailId"] as Number;
            var _loc_3:* = event.data["index"] as int;
            GameProxy.mailProxy.getMailAttachment(_loc_2, _loc_3);
            
        }

        private function queryMail(event:DataEvent) : void
        {
            var _loc_2:* = event.data["condition"];
            var _loc_3:* = event.data["type"];
            var _loc_4:* = event.data["status"];
            var _loc_5:* = event.data["startIndex"];
            GameProxy.mailProxy.queryMail(_loc_2, _loc_3, _loc_4, _loc_5);
            
        }

        private function readMail(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Number;
            GameProxy.mailProxy.readMail(_loc_2);
            
        }

        private function removeMail(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            GameProxy.mailProxy.removeMail(_loc_2);
            
        }

        private function sendMail(event:DataEvent) : void
        {
            var _loc_2:* = event.data["smail"] as SMail;
            var _loc_3:* = event.data["uids"] as Dictionary;
            var _loc_4:* = _loc_2.toPlayerName;
            var _loc_5:* = _loc_2.title;
            var _loc_6:* = _loc_2.content;
            var _loc_7:* = _loc_2.attachmentCoin;
            var _loc_8:* = _loc_2.attachmentGold;
            GameProxy.mailProxy.sendMail(_loc_4, _loc_5, _loc_6, _loc_7, _loc_8, _loc_3);
            
        }

        private function onMailQuerySuccess(event:DataEvent) : void
        {
            this._mailModule.updateMailListProvider();
            
        }

        public function onNewMailComeHandler(event:DataEvent) : void
        {
            _isHaveNew = true;
            
        }

        public function onMailSendToPlayerHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameController.mail.view.show();
            this._mailModule.writeMailToPlayer(_loc_2);
            
        }

        public function get isViewShow() : Boolean
        {
            return this._mailModule && this._mailModule.isHide == false;
        }

    }
}
